/**
 * @fileoverview Scripts for weight log
 * @author scottakirkwood@gmail.com (Scott Kirkwood)
 */

google.load('dojo', '1.3.0');

function init() {
  var adate = dojo.byId('date');
  if (!adate.value) {
    var today = new Date();
    mo = '' + (today.getMonth() + 1);
    if (mo.length == 1) {
      mo = '0' + mo;
    }
    dt = '' + today.getDate();
    if (dt.length == 1) {
      dt = '0' + dt;
    }
    var str = today.getFullYear() + '-' + mo + '-' + dt;
    adate.value = str;
  }
}

/**
 * Add an entry to the database.
 */
addEntry = function() {
  var response = dojo.byId('response');

  var xhrData = {
    url: '/add',
    handleAs: 'json',
    timeout: 10000, // in milliseconds
    sync: true,
    content: {
      date: dojo.byId('date').value,
      weight_kg: dojo.byId('weight_kg').value,
      workout_min: dojo.byId('workout_min').value,
      running_km: dojo.byId('running_km').value,
      sld_cal: dojo.byId('sld_cal').value,
    },
    handle: function(data) {
      if (data.ok) {
        response.innerHTML = 'Saved.';
        defaultValues();
      } else {
        if (data.error) {
          response.innerHTML = data.error;
        } else {
          response.innerHTML = 'Error';
        }
      }
    },
  }
  response.innerHTML = 'Saving...'
  dojo.xhrPost(xhrData);
}

/**
 * Set the values to their defaults.
 */
defaultValues = function() {
}
